<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统进程监控</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .button-group {
            text-align: center;
            margin-bottom: 20px;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            margin: 0 10px;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .info-section {
            margin-bottom: 30px;
        }
        .info-section h2 {
            color: #555;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
        }
        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 15px;
        }
        .info-item {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            border-left: 4px solid #007bff;
        }
        .info-label {
            font-weight: bold;
            color: #333;
            margin-bottom: 5px;
        }
        .info-value {
            color: #666;
            word-break: break-all;
        }
        .process-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
        }
        .process-table th,
        .process-table td {
            border: 1px solid #ddd;
            padding: 8px 12px;
            text-align: left;
        }
        .process-table th {
            background-color: #007bff;
            color: white;
            font-weight: bold;
        }
        .process-table tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        .process-table tr:hover {
            background-color: #e9ecef;
        }
        .loading {
            text-align: center;
            color: #666;
            font-style: italic;
        }
        .error {
            color: #dc3545;
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            padding: 10px;
            border-radius: 5px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>系统进程监控</h1>
        
        <div class="button-group">
            <button onclick="getSystemInfo()">获取系统信息</button>
            <button onclick="getProcessList()">获取进程列表</button>
            <button onclick="refreshAll()">刷新所有</button>
        </div>
        
        <div class="info-section">
            <h2>系统信息</h2>
            <div id="systemInfo" class="loading">点击"获取系统信息"按钮查看系统状态</div>
        </div>
        
        <div class="info-section">
            <h2>进程列表</h2>
            <div id="processList" class="loading">点击"获取进程列表"按钮查看进程信息</div>
        </div>
    </div>

    <script>
        async function getSystemInfo() {
            const systemInfoDiv = document.getElementById('systemInfo');
            systemInfoDiv.innerHTML = '<div class="loading">正在获取系统信息...</div>';
            
            try {
                const response = await fetch('/systemInfo', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                
                const data = await response.json();
                
                if (data.success) {
                    const info = data.data;
                    let html = '<div class="info-grid">';
                    
                    // 系统基本信息
                    html += `<div class="info-item">
                        <div class="info-label">操作系统</div>
                        <div class="info-value">${info.osName} ${info.osVersion}</div>
                    </div>`;
                    
                    html += `<div class="info-item">
                        <div class="info-label">架构</div>
                        <div class="info-value">${info.osArch}</div>
                    </div>`;
                    
                    html += `<div class="info-item">
                        <div class="info-label">处理器数量</div>
                        <div class="info-value">${info.availableProcessors}</div>
                    </div>`;
                    
                    html += `<div class="info-item">
                        <div class="info-label">系统负载</div>
                        <div class="info-value">${info.systemLoadAverage ? info.systemLoadAverage.toFixed(2) : 'N/A'}</div>
                    </div>`;
                    
                    html += `<div class="info-item">
                        <div class="info-label">进程总数</div>
                        <div class="info-value">${info.processCount}</div>
                    </div>`;
                    
                    html += `<div class="info-item">
                        <div class="info-label">线程总数</div>
                        <div class="info-value">${info.threadCount}</div>
                    </div>`;
                    
                    // 内存信息
                    const heapUsedMB = Math.round(info.heapMemoryUsed / 1024 / 1024);
                    const heapMaxMB = Math.round(info.heapMemoryMax / 1024 / 1024);
                    const heapCommittedMB = Math.round(info.heapMemoryCommitted / 1024 / 1024);
                    
                    html += `<div class="info-item">
                        <div class="info-label">堆内存使用</div>
                        <div class="info-value">${heapUsedMB}MB / ${heapMaxMB}MB</div>
                    </div>`;
                    
                    html += `<div class="info-item">
                        <div class="info-label">堆内存已提交</div>
                        <div class="info-value">${heapCommittedMB}MB</div>
                    </div>`;
                    
                    const nonHeapUsedMB = Math.round(info.nonHeapMemoryUsed / 1024 / 1024);
                    const nonHeapMaxMB = info.nonHeapMemoryMax > 0 ? Math.round(info.nonHeapMemoryMax / 1024 / 1024) : 'N/A';
                    
                    html += `<div class="info-item">
                        <div class="info-label">非堆内存使用</div>
                        <div class="info-value">${nonHeapUsedMB}MB / ${nonHeapMaxMB}MB</div>
                    </div>`;
                    
                    html += '</div>';
                    systemInfoDiv.innerHTML = html;
                } else {
                    systemInfoDiv.innerHTML = `<div class="error">获取系统信息失败: ${data.message}</div>`;
                }
            } catch (error) {
                systemInfoDiv.innerHTML = `<div class="error">请求失败: ${error.message}</div>`;
            }
        }
        
        async function getProcessList() {
            const processListDiv = document.getElementById('processList');
            processListDiv.innerHTML = '<div class="loading">正在获取进程列表...</div>';
            
            try {
                const response = await fetch('/processList', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                
                const data = await response.json();
                
                if (data.success) {
                    const processes = data.data;
                    
                    if (processes.length === 0) {
                        processListDiv.innerHTML = '<div class="loading">没有找到进程信息</div>';
                        return;
                    }
                    
                    let html = '<table class="process-table">';
                    html += '<thead><tr>';
                    html += '<th>PID</th>';
                    html += '<th>命令</th>';
                    html += '<th>用户</th>';
                    html += '<th>CPU时间(ms)</th>';
                    html += '<th>启动时间</th>';
                    html += '<th>状态</th>';
                    html += '</tr></thead>';
                    html += '<tbody>';
                    
                    processes.forEach(process => {
                        const startTime = process.startTime ? new Date(process.startTime).toLocaleString() : 'N/A';
                        const cpuTime = process.totalCpuTime || 0;
                        const status = process.alive ? '运行中' : '已停止';
                        
                        html += '<tr>';
                        html += `<td>${process.pid}</td>`;
                        html += `<td>${process.command}</td>`;
                        html += `<td>${process.user}</td>`;
                        html += `<td>${cpuTime}</td>`;
                        html += `<td>${startTime}</td>`;
                        html += `<td>${status}</td>`;
                        html += '</tr>';
                    });
                    
                    html += '</tbody></table>';
                    processListDiv.innerHTML = html;
                } else {
                    processListDiv.innerHTML = `<div class="error">获取进程列表失败: ${data.message}</div>`;
                }
            } catch (error) {
                processListDiv.innerHTML = `<div class="error">请求失败: ${error.message}</div>`;
            }
        }
        
        function refreshAll() {
            getSystemInfo();
            getProcessList();
        }
        
        // 页面加载时自动获取系统信息
        window.onload = function() {
            getSystemInfo();
        };
    </script>
</body>
</html> 